<template>
  <view class="integral-ranking">
    <view class="fixed-header">
      <image src="/static/img/rank-bg.png" class="integral-bg" />
      <view class="integral-head">
        <view class="status_bar">
          <view class="top_view tab-bar-show">
            <view class="btn-back" @click="goBack()">
              <image src="/static/icon/iconfont-back.png" style="width: 20upx; height: 34upx;" />
            </view>
            <view class="tabbar-title">积分排行</view>
            <view class="type-group">
              <view class="type-item left" :class="{'active': type === 0}" @click="changeType(0)">个人</view>
              <view
                class="type-item right"
                :class="{'active': type === 1}"
                @click="changeType(1)"
              >部门</view>
            </view>
          </view>
        </view>

        <view class="avatar" :style="{backgroundImage: 'url(/static/img/avatar-bg.png)'}">
          <image v-if="myInfo.headUrl" class="avatar-icon" :src="myInfo.headUrl" />
          <image v-else class="avatar-icon" src="/static/logo/logo1024x1024.png" />
        </view>
        <view class="ranking" :style="{backgroundImage: 'url(/static/img/ranking-bg.png)'}">
          <span v-if="myInfo.rank <= 0">暂无排名</span>
          <span v-else>第{{myInfo.rank}}名</span>
        </view>
        <view class="name">{{myInfo.deptName}} - {{myInfo.realName}}</view>
      </view>

      <view class="time-group">
        <view
          class="time-item"
          :class="{active: item.id === lengthOfTimeId}"
          v-for="item in timeGroup"
          :key="item.id"
          @click="onLengthOfTimeChange(item.id)"
        >{{item.label}}</view>
      </view>
    </view>

    <view class="ranking-list">
      <view class="ranking-item" v-for="item in rankingList" :key="item.rank">
        <view class="rank">
          <image
            v-if="item.rank === 1"
            src="/static/icon/icon-first.png"
            style="width:50upx;height:65upx;"
          />
          <image
            v-if="item.rank === 2"
            src="/static/icon/icon-second.png"
            style="width:50upx;height:65upx;"
          />
          <image
            v-if="item.rank === 3"
            src="/static/icon/icon-third.png"
            style="width:50upx;height:65upx;"
          />
          <span v-if="item.rank >= 4">{{item.rank}}</span>
        </view>

        <!-- 个人排行模板 -->
        <template v-if="type === 0">
          <template>
            <image v-if="item.headUrl" class="icon" :src="item.headUrl" />
            <image v-else class="icon" src="/static/logo/logo1024x1024.png" />
          </template>
          <view class="name">
            <view class="user-name">{{item.realName}}</view>
            <view>{{item.deptName}}</view>
          </view>
          <view class="right">
            <view class="rank-text" :class="{'top-three': item.rank < 4}">第{{item.rank}}名</view>
            <view class="grade">{{item.grade}}分</view>
          </view>
        </template>
        <!-- 部门排行模板 -->
        <template v-else>
          <view class="name">{{item.deptName}}</view>
          <view class="right">
            <view class="rank-text" :class="{'top-three': item.rank < 4}">第{{item.rank}}名</view>
            <view class="grade">{{item.avgGrade}}分</view>
          </view>
        </template>
      </view>
    </view>
  </view>
</template>

<script>
import { post_md5 } from '../../common/request'
export default {
  name: 'integralRanking',
  data() {
    return {
      bgRanking: '/static/img/ranking-bg.png',
      myInfo: {
        realName: '',
        deptName: '',
        headUrl: '',
        userRank: 0
      },
      rankingList: [],
      lastRankingList: [],
      lengthOfTimeId: 0,
      timeGroup: [
        { label: '日', id: 0 },
        { label: '周', id: 1 },
        { label: '月', id: 2 },
        { label: '季度', id: 3 },
        { label: '年', id: 4 }
      ],
      type: 0
    }
  },
  methods: {
    goBack() {
      uni.navigateBack()
    },
    onLengthOfTimeChange(id) {
      this.lengthOfTimeId = id
      this.loadRanking()
    },
    async loadRanking() {
      this.rankingList = []
      let res = await post_md5(
        '/integral-app/integral/usergrade/usergraderank',
        {
          userId: uni.getStorageSync('userId'),
          type: this.type,
          rankType: this.lengthOfTimeId
        }
      )
      this.rankingList = res.data.frontRankList
      this.lastRankingList = res.data.behindRankList
      this.myInfo.realName = res.data.realName
      this.myInfo.deptName = res.data.deptName
      this.myInfo.headUrl = res.data.headUrl
      this.myInfo.rank = res.data.userRank
    },
    changeType(type) {
      this.type = type
      this.loadRanking()
    }
  },
  created() {
    this.loadRanking()
  }
}
</script>

<style lang="less" scoped>
.integral-ranking {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;

  .fixed-header {
    position: fixed;
    height: 600upx;
    width: 100%;
    z-index: 100;

    .integral-bg {
      width: 100%;
      height: 600upx;
      display: block;
    }

    .integral-head {
      position: absolute;
      left: 0;
      top: 0;
      height: 600upx;
      width: 100%;

      .status_bar {
        height: var(--status-bar-height);
        width: 100%;
        background: rgba(255, 255, 255, 0);
        position: absolute;
        transition: all 0.5s;

        .top_view {
          height: var(--status-bar-height);
          width: calc(~'100% - 20px');
          position: fixed;
          background: rgba(255, 255, 255, 0);
          top: var(--status-bar-height);
          z-index: 999;
          transition: all 0.5s;
          height: 40px;
          display: flex;
          align-items: center;
          margin: 0 20upx;
          justify-content: space-between;

          .btn-back {
            width: 70upx;
            height: 34upx;
            z-index: 10;
          }

          .tabbar-title {
            z-index: 1;
            color: white;
            position: absolute;
            width: 100%;
            text-align: center;
          }

          .type-group {
            z-index: 10;
            display: flex;
            justify-content: start;

            .left {
              border-top-left-radius: 15px;
              border-bottom-left-radius: 15px;
            }

            .right {
              border-top-right-radius: 15px;
              border-bottom-right-radius: 15px;
            }

            .type-item {
              width: 104upx;
              height: 40upx;
              line-height: 40upx;
              font-size: 28upx;
              text-align: center;
              color: white;
              border: 1px solid #02a8f4;
              background: #02a8f4;

              &.active {
                color: #999;
                border: 1px solid #fff;
                background-color: #fff;
                box-shadow: 0px 0px 16px 0px rgba(225, 225, 225, 0.5);
              }
            }
          }
        }
      }

      .avatar {
        background: url('/static/img/avatar-bg.png');
        background-size: 100% 100%;
        background-repeat: no-repeat;
        width: 284upx;
        height: 216upx;
        margin: auto;
        margin-top: 160upx;

        .avatar-icon {
          width: 192upx;
          height: 192upx;
          border-radius: 50%;
          margin: auto;
          display: block;
          margin-bottom: 20upx;
        }
      }

      .ranking {
        background-size: 100% 100%;
        background-repeat: no-repeat;
        width: 402upx;
        height: 82upx;
        line-height: 90upx;
        font-size: 40upx;
        font-weight: bold;
        color: #000;
        margin: auto;
        text-align: center;
        margin-top: 20upx;
      }

      .name {
        margin-top: 30upx;
        text-align: center;
        color: white;
      }
    }

    .time-group {
      height: 120upx;
      padding: 0 40upx;
      display: flex;
      justify-content: space-around;
      align-items: center;
      background-color: #fff;
      border-bottom: 1px solid #eee;

      .time-item {
        width: 104upx;
        height: 50upx;
        line-height: 50upx;
        font-size: 28upx;
        text-align: center;
        border: 1px solid #999;
        color: #999;
        border-radius: 25px;

        &.active {
          color: #fff;
          border: 1px solid #02a8f4;
          background: #02a8f4;
          box-shadow: 0px 0px 16px 0px rgba(2, 168, 244, 0.5);
          border-radius: 25px;
        }
      }
    }
  }

  .ranking-list {
    z-index: 99;
    margin-top: 720upx;
    padding: 20upx 40upx;

    .ranking-item {
      padding: 30upx 0;
      display: flex;
      align-items: center;
      border-bottom: 1px solid #eee;

      .rank {
        width: 70upx;
        font-size: 48upx;
        font-weight: 400;
        color: #999;
        text-align: center;
      }

      .icon {
        margin-left: 40upx;
        height: 90upx;
        width: 90upx;
        border-radius: 50%;
      }

      .name {
        margin-left: 40upx;
        font-size: 28upx;
        font-weight: 400;
        color: #999;
        flex: auto;

        .user-name {
          margin-bottom: 2px;
          color: #0a90cf;
        }
      }

      .right {
        text-align: center;
        .rank-text {
          font-size: 30upx;
          font-weight: 400;
          color: #999;

          &.top-three {
            color: rgba(253, 150, 67, 1);
          }
        }

        .grade {
          font-size: 24upx;
          font-weight: 400;
          color: #999;
          margin-top: 5upx;
        }
      }
    }
  }
}
</style>